package org.bugjail.dataaccess.jdbc.objects;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.bugjail.data.Project;
import org.bugjail.dataaccess.jdbc.connection.QueryException;
import org.bugjail.dataaccess.jdbc.connection.QueryManager;
import org.bugjail.dataaccess.objects.ProjectDAO;

class JDBCProjectDAO implements ProjectDAO {
	
	private final String PROPERTIES_FILE = "/org/bugjail/dataaccess/jdbc/properties/projectdao.properties";
	
	private final String PROPERTY_INSERT = "insert";
	
	private final String PROPERTY_UPDATE = "update";
	
	private final String PROPERTY_DELETE = "delete";
	
	//private final String PROPERTY_RETRIEVE_BY_PERSON = "retrieve.byperson";
	private final String PROPERTY_RETRIEVE_BY_ID = "retrieve.byid";
	private final String PROPERTY_RETRIEVE_ALL = "retrieve.all";
	private final String PROPERTY_RETRIEVE_BY_NAME = "retrieve.byname";
	
	@Override
	public void create(Project element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_INSERT);
				statement.setString(1, element.getName());
				statement.setString(2, element.getDescription());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public void delete(Project element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_DELETE);
				statement.setInt(1, element.getId());
				statement.executeUpdate();
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public Project retrieveById(int id) {
		Project project = null;
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_ID);
				statement.setInt(1, id);
				ResultSet rs = statement.executeQuery();
				if (rs.next()) {
					project = extractProjectsFromResultSet(rs, queryManager);
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return project;
	}

	@Override
	public List<Project> retrieveByName(String name) {
		name = "%" + name + "%";
		List<Project> projects = new LinkedList<Project>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_NAME);
				statement.setString(1, name);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					projects.add(extractProjectsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return projects;
	}

	@Override
	public void update(Project element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_UPDATE);
				statement.setString(1, element.getName());
				statement.setString(2, element.getDescription());
				statement.setInt(3, element.getId());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public List<Project> retrieveAll() {
		List<Project> projects = new LinkedList<Project>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_ALL);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					projects.add(extractProjectsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return projects;
	}

	/*@Override
	public List<Project> retrieveByPerson(Person person) {
		List<Project> projects = new LinkedList<Project>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_PERSON);
				statement.setInt(1, person.getId());
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					projects.add(extractProjectsIdsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return projects;
	}*/
	
	//---------------------------------- PRIVATE METHODS ----------------------------------//

	private Project extractProjectsFromResultSet(ResultSet rs, QueryManager queryManager) throws SQLException {
		Project project = new Project();
		project.setId(rs.getInt("id"));
		project.setName(rs.getString("name"));
		project.setDescription(rs.getString("description"));
		return project;
	}

	/*private Project extractProjectsIdsFromResultSet(ResultSet rs, QueryManager queryManager) throws SQLException {
		int idProjects = rs.getInt("project_id");		
		return this.retrieveById(idProjects);
	}*/

}